package com.example.mac.yuejian14.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import com.example.mac.yuejian14.R;
import com.example.mac.yuejian14.activity.ClubIntroActivity;
import com.example.mac.yuejian14.bean.Club;

import static com.example.mac.yuejian14.Const_Pic.BASE_URL;
/**
 * Created by GaoNa on 2018/10/8.
 */
public class ClubListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<Club> mDataList;
    private LayoutInflater mLayoutInflater;

    public ClubListAdapter(Context mContext, List<Club> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public ClubListAdapter(Context context) {

        this(context, new ArrayList<Club>());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.item_clublist, parent, false);
        return new ViewHolder(mContext, v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (mDataList == null || mDataList.isEmpty()) {
            return;
        }

        final Club entity = mDataList.get(position);

        if (null == entity) {
            return;
        }

        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setView(entity);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(mContext, ClubIntroActivity.class);
                intent.putExtra("clubid",entity.getClubid());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public Club getItem(int position) {
        return mDataList == null ? null : mDataList.get(position);
    }

    public void setData(List<Club> list) {
        this.mDataList = list;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private static ImageView club_pic;
        Context mContext;
        TextView club_name;

        public ViewHolder(Context context, View itemView) {
            super(itemView);
            mContext = context;
            club_name = (TextView) itemView.findViewById(R.id.club_name);
            club_pic = (ImageView) itemView.findViewById(R.id.club_pic);
        }

        void setView(Club entity) {

            club_name.setText(entity.getClubname());
            String club_pic = BASE_URL + entity.getClubpic();
            if (!club_pic.equals("")) {
                Picasso.with(mContext).load(club_pic).error(R.drawable.no_pic).into(ViewHolder.club_pic);
            }
        }

    }
}
